<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date :disabled=formDisabled placeholder="请选择创建日期" v-model="model.createTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerId">
              <!--              <a-input :disabled=formDisabled type="list" v-model="model.handlerId" dictCode="erp_employee_file,name,id"-->
              <!--                       placeholder="请选择经办人"></a-input>-->
              <!--            </a-form-model-item>-->
              <select-person-input
                :disabled=formDisabled
                v-model="model.handlerId" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="经办部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerDeptId">
              <!--              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.handlerDeptId"-->
              <!--                                 dict="sys_depart,id,depart_name" placeholder="请选择经办部门"/>-->
              <j-select-depart :disabled=formDisabled v-model="model.handlerDeptId"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="印章编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealQualificationId">
              <a-input :disabled=formDisabled v-model="model.sealQualificationId" placeholder="请输入印章编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="印章类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealType">
              <j-dict-select-tag :disabled=formDisabled type="list" :sealName.sync="sealName" v-model="model.sealType"
                                 dictCode="yzlx_type"
                                 placeholder="请选择印章类别"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="印章所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealCompany">
              <j-dict-select-tag :disabled=formDisabled type="list" :companyName.sync="companyName"
                                 v-model="model.sealCompany"
                                 dictCode="erp_company_files,file_name,id" placeholder="请选择印章所属公司"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input :disabled=formDisabled v-model="model.name" placeholder="请输入名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="印章负责人" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="sealCertificationHead">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.sealCertificationHead"
                                 dictCode="erp_employee_file,name,id" placeholder="请选择印章负责人"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="授权责任人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="authorizedPerson">
              <!--              <a-input :disabled='true' v-model="model.authorizedPerson" placeholder="请输入授权责任人"></a-input>-->
              <select-person-input
                :disabled=formDisabled
                v-model="model.handlerId" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="委托方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delegate">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.delegate"
                                 dictCode="erp_company_files,file_name,id" placeholder="请选择委托方"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="被委托方（印章保管人）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trustee">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.trustee"
                                 dictCode="erp_employee_file,name,id" placeholder="请选择被委托方（印章保管人）"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="委托开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entrustDateStart">
              <j-date :disabled=formDisabled placeholder="请选择委托开始时间" v-model="model.entrustDateStart"
                      style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="委托结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entrustDateEnd">
              <j-date :disabled=formDisabled placeholder="请选择委托结束时间" v-model="model.entrustDateEnd"
                      style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="印章使用范围" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="sealCertificationUseScope">
              <a-input :disabled=formDisabled v-model="model.sealCertificationUseScope"
                       placeholder="请输入印章使用范围"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="注意事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attentionMatters">
              <a-input :disabled=formDisabled v-model="model.attentionMatters" placeholder="请输入注意事项"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上传附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upload">
              <j-upload :disabled=formDisabled v-model="model.upload"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import JDictNameTag from '@views/erp/administration/seal/modules/JDictNameTag.vue'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import { mapGetters } from 'vuex';
import moment from 'moment';

export default {
  name: 'ErpSealCertificateFileManageForm',
  components: {
    JDictNameTag,
    SelectPersonInput
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      sealName: '',
      companyName: '',
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        departmentId: [
          { required: true, message: '请输入经办部门!' },
        ],
        sealType: [
          { required: true, message: '请输入印章类别!' },
        ],
        sealCompany: [
          { required: true, message: '请输入印章所属公司!' },
        ],
        sealCertificationHead: [
          { required: true, message: '请输入印章负责人!' },
        ],
      },
      url: {
        add: '/api/erp/administration/seal/erpSealCertificateFileManage/add',
        edit: '/api/erp/administration/seal/erpSealCertificateFileManage/edit',
        queryById: '/api/erp/administration/seal/erpSealCertificateFileManage/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    ...mapGetters(['userInfo']),
    sealCompanyChange(value) {
      this.model.name = this.companyName + this.sealName
    },
    sealTypeChange(value) {
      this.model.name = this.companyName + this.sealName
    },
    add() {
      this.edit(this.modelDefault);
      this.model.handlerId = this.userInfo().empId;
      this.model.handlerDeptId = this.userInfo().deptId;
      this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss');
      // this.model.applyPreson=this.userInfo().empId;
      // this.model.applyDept=this.userInfo().deptId;

      this.model.authorizedPerson = this.userInfo().empId;
    },
    edit(record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>
